/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJFrame.java
 *
 * Created on Jul 28, 2012, 8:42:07 PM
 */
package epibot.testeProteinas;

import java.io.File;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Rafael Tosta
 */
public class JFrameResultadosTeste extends javax.swing.JFrame {

    private ArrayList<EpitopoGeral> temp;

    /** Creates new form NewJFrame */
    public JFrameResultadosTeste(ArrayList<EpitopoGeral> temp) {
        initComponents();
        this.setExtendedState(MAXIMIZED_BOTH);

        this.temp = temp;
        this.tabelaDeDados.setEnabled(false);
        this.tabelaDeDados.setModel(this.addDadosDosResultados(getDados(temp)));

    }

    public DefaultTableModel iniciaTabelaPadrao() {
        String s[] = new String[]{""};
        return new javax.swing.table.DefaultTableModel(null, s);
    }

    private DefaultTableModel addDadosDosResultados(String[][] dados) {
        String c[];

        c = new String[]{"RANK", "EPITOPE", "POSITION", "GLOBAL_SCORE", "ALLELE", "SIZE", "HEADER"};

        // da pra verificar qual é o arquivo por meio de um flag e selecionar o cabeçalho
        return new javax.swing.table.DefaultTableModel(dados, c);
    }

    /**
     * Obtém os dados do arquivo e armazena em uma matriz
     *
     * @param file Referência do arquivo
     * @return
     */
    private String[][] getDados(ArrayList<EpitopoGeral> temp) {


        int linha = temp.size();
        int coluna = 7;
        String[][] campos = new String[linha][coluna];

        for (int j = 0; j < temp.size(); j++) {
            String[] split = temp.get(j).getInformacaoEG().split(",");
            //coloca os valores na matriz
            for (int k = 0; k < coluna; k++) {
                campos[j][k] = split[k];//pedaco[k];               
            }
        }

        return campos;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel8 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        tabelaDeDados = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel8.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jPanel8.setLayout(new javax.swing.BoxLayout(jPanel8, javax.swing.BoxLayout.LINE_AXIS));

        tabelaDeDados.setModel(this.iniciaTabelaPadrao());
        tabelaDeDados.setCellSelectionEnabled(true);
        tabelaDeDados.setMaximumSize(new java.awt.Dimension(0, 20));
        tabelaDeDados.getTableHeader().setReorderingAllowed(false);
        tabelaDeDados.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tabelaDeDadosMousePressed(evt);
            }
        });
        jScrollPane4.setViewportView(tabelaDeDados);

        jPanel8.add(jScrollPane4);

        getContentPane().add(jPanel8, java.awt.BorderLayout.CENTER);

        jMenu1.setText("File");

        jMenuItem1.setText("Save");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed

    String nome = JOptionPane.showInputDialog(this,"File name:","EPIBOT", JOptionPane.QUESTION_MESSAGE);
    if (nome == null) {
        return;
    }

    if (nome.equals("")) {
        JOptionPane.showMessageDialog(null, "Invalid name!", "EPIBOT alert", JOptionPane.ERROR_MESSAGE);
    } else {

        JFileChooser e = new JFileChooser();
        e.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        File file = null;
        int returnVal = e.showSaveDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) { //se escolhido o diretório...
            file = e.getSelectedFile();
        }

        if (file != null) {
            ProcessadorDeArquivos pda = new ProcessadorDeArquivos();
            pda.escritaResultadoGlobal(temp, file, nome);
        }
    }
}//GEN-LAST:event_jMenuItem1ActionPerformed

private void tabelaDeDadosMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaDeDadosMousePressed
// TODO add your handling code here:
}//GEN-LAST:event_tabelaDeDadosMousePressed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTable tabelaDeDados;
    // End of variables declaration//GEN-END:variables
}
